from threading import Thread
from tkinter import ttk
from tkinter import *
from Config import modeList

'''
* tkinter docs
* https://docs.python.org/zh-cn/3.9/library/tkinter.html#
* http://www.wb98.com/category-12.html
'''

class RenderContent:
    def __init__(self, root):
        self.root = root
        self.createModeType()
        self.note.select(0)

    def createModeType(self):
        note = ttk.Notebook(self.root, padding=10)
        note.pack(fill=BOTH, expand=True)
        for item in modeList:
            div = Frame(self.root)
            mode = item['mode']
            mode.createConfig(div)
            note.add(div, text=item['name'])

        self.note = note

    def get_mode(self):
        num = self.note.index('current')
        return int(num)

    # 停止
    def stop(self):
        mode = self.get_mode()
        instance = modeList[mode]['mode']
        instance.stop = True
        instance.isEnd = True

    # 开始
    def start(self):
        mode = self.get_mode()
        instance = modeList[mode]['mode']
        if instance.isEnd == False:
            print('还有没跑完的')
            return

        # 多线程防阻塞卡死界面
        t = Thread(target=instance.runMain)
        # 设置守护，如果主线程结束，子线程也随之结束
        t.setDaemon(True)
        # 启动
        t.start()
